Skip to content

fix: respect AbortSignal in run() - throw on abort instead of silently returning#376

Open
ctonneslan wants to merge 1 commit intoreplicate:mainfrom
ctonneslan:fix/abort-signal-run
Open

fix: respect AbortSignal in run() - throw on abort instead of silently returning#376
ctonneslan wants to merge 1 commit intoreplicate:mainfrom
ctonneslan:fix/abort-signal-run

Conversation

@ctonneslan
Copy link

Problem

replicate.run() ignores AbortSignal in two ways:

  1. Pre-aborted signal: If signal.aborted is already true before calling run(), the SDK starts the prediction anyway instead of throwing immediately.

  2. Mid-poll abort: If the signal becomes aborted during polling, run() stops polling and cancels the prediction, but then silently returns instead of throwing AbortError. The caller has no way to distinguish between a successful completion and a cancelled request.

Fix

Before starting: Check signal.aborted and throw AbortError immediately if already aborted.

After cancellation: Throw AbortError instead of returning the prediction output. The predictions.cancel() call is wrapped in .catch() to handle cases where the prediction already completed on Replicate's servers.

// Pre-aborted check
if (signal?.aborted) {
  const error = new Error("Aborted");
  error.name = "AbortError";
  throw error;
}

// Post-cancellation throw (instead of silent return)
if (signal && signal.aborted) {
  await this.predictions.cancel(prediction.id).catch(() => {});
  const error = new Error("Aborted");
  error.name = "AbortError";
  throw error;
}

This matches how fetch() handles AbortSignal and allows callers to properly distinguish cancelled requests from completed ones.

Fixes #370

… abort

run() ignores AbortSignal in two ways:
1. If signal is already aborted before calling run(), the prediction
   starts anyway instead of throwing immediately
2. If signal becomes aborted during polling, run() cancels the
   prediction but returns the result instead of throwing AbortError

Now checks signal.aborted before starting the prediction and throws
AbortError after cancellation instead of silently returning output.
The cancel call is wrapped in catch to handle cases where the
prediction already completed on Replicate's servers.

Fixes replicate#370
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

replicate.run() ignores AbortSignal - doesn't throw when signal is already aborted or becomes aborted during polling

1 participant